#include "piece.h"
#include "echiquier.h"
#include "jeu.h"

using namespace std;

// constructeurs
Echiquier::Echiquier(): tour(0)
{
	for(int i = 0;i<6;i++)
	{
		for(int j = 0; j<8; j++)
		{
			for (int k = 0; k<6; k++)
			{
				this->echiquier[i][j][k] = new Piece();
			}
		}
	}
}

void Echiquier::initialise()
{
	int i,j;
	Piece * p;
	for(i = 0; i<6; i++)
	{
		for(j = 0; j<6; j++)
		{
			p = new Piece("Pion", 1, 0, 0, 0, 0);
			this->put_piece(p, i, 1,j);

			p = new Piece("Pion", 0, 0, 0, 0, 0);
			this->put_piece(p, i, 6,j);
		}
	}
	
	p = new Piece("Tour", 1, 0, 0, 0, 0);
	this->put_piece(p, 0, 0, 5);
	
	p = new Piece("Tour", 1, 0, 0, 0, 0);
	this->put_piece(p, 0, 0, 0);

	p = new Piece("Tour", 1, 0, 0, 0, 0);
	this->put_piece(p, 5, 0, 5);
	
	p = new Piece("Tour", 1, 0, 0, 0, 0);
	this->put_piece(p, 5, 0, 0);
	
	p = new Piece("Cavalier", 1, 0, 0, 0, 0);
	this->put_piece(p, 1, 0, 1);
	
	p = new Piece("Cavalier", 1, 0, 0, 0, 0);
	this->put_piece(p, 4, 0, 1);
	
	p = new Piece("Cavalier", 1, 0, 0, 0, 0);
	this->put_piece(p, 4, 0, 4);
	
	p = new Piece("Cavalier", 1, 0, 0, 0, 0);
	this->put_piece(p, 1, 0, 4);
	
	p = new Piece("Fou", 1, 0, 0, 0, 0);
	this->put_piece(p, 2, 0, 2);
	
	p = new Piece("Fou", 1, 0, 0, 0, 0);
	this->put_piece(p, 3, 0, 2);
	
	p = new Piece("Dame", 1, 0, 0, 0, 0);
	this->put_piece(p, 3, 0, 3);
	
	p = new Piece("Roi", 1, 0, 0, 0, 0);
	this->put_piece(p, 2, 0, 3);
	
	
	p = new Piece("Tour", 0, 0, 0, 0, 0);
	this->put_piece(p, 0, 7, 5);
	
	p = new Piece("Tour", 0, 0, 0, 0, 0);
	this->put_piece(p, 0, 7, 0);

	p = new Piece("Tour", 0, 0, 0, 0, 0);
	this->put_piece(p, 5, 7, 5);
	
	p = new Piece("Tour", 0, 0, 0, 0, 0);
	this->put_piece(p, 5, 7, 0);
	
	p = new Piece("Cavalier", 0, 0, 0, 0, 0);
	this->put_piece(p, 1, 7, 1);
	
	p = new Piece("Cavalier", 0, 0, 0, 0, 0);
	this->put_piece(p, 4, 7, 1);
	
	p = new Piece("Cavalier", 0, 0, 0, 0, 0);
	this->put_piece(p, 4, 7, 4);
	
	p = new Piece("Cavalier", 0, 0, 0, 0, 0);
	this->put_piece(p, 1, 7, 4);
	
	p = new Piece("Fou", 0, 0, 0, 0, 0);
	this->put_piece(p, 2, 7, 2);
	
	p = new Piece("Fou", 0, 0, 0, 0, 0);
	this->put_piece(p, 3, 7, 2);
	
	p = new Piece("Dame", 0, 0, 0, 0, 0);
	this->put_piece(p, 2, 7, 3);
	
	p = new Piece("Roi", 0, 0, 0, 0, 0);
	this->put_piece(p, 3, 7, 3);
}

//destructeur
Echiquier::~Echiquier()
{
	for(int i = 0;i<6;i++)
	{
		for(int j = 0; j<8; j++)
		{
			for (int k = 0; k<6; k++)
			{
				delete this->echiquier[i][j][k];
			}
		}
	}
}
//utilitaires
void Echiquier::trouve_piece(string nom, int couleur, int * res)
{
	int i,j,k;
	for(i=0; i<6; i++)
	{
		for(j=0; j<8; j++)
		{
			for(k=0; k<6; k++)
			{
				if((this->get_piece(i,j,k)->get_nom().compare("Roi") == 0) && (this->get_piece(i,j,k)->get_couleur() == couleur))
				{
					res[0] = i;
					res[1] = j;
					res[2] = k;
					return ;
				}
			}
		}
	}
}

void Echiquier::est_echec(bool* res)
{
	int pos[3] = {-1, -1, -1};
	this->trouve_piece("Roi", 0, pos);
	
	vector<int> *tab1;
	vector<int> *tab2;
	
	zones_couvertes(tab1, tab2, this);

	if (appartient(pos[0], pos[1], pos[2], tab2))
	{
		res[0] = true;
	}

	this->trouve_piece("Roi", 1, pos);
	
	if (appartient(pos[0], pos[1], pos[2], tab1))
	{
		res[1] = true;
	}

}

void Echiquier::trace(GLUquadric* params, TrackBallCamera * camera)
{
	dessine_echiquier();
	for(int i = 0;i<6;i++)
	{
		for(int j = 0; j<8; j++)
		{
			for (int k = 0; k<6; k++)
			{
				this->echiquier[i][j][k]->trace(params);
			}
		}
	}
}

void Echiquier::put_piece(Piece *p, int xa, int ya, int za)
{
	delete this->echiquier[xa][ya][za];
	this->echiquier[xa][ya][za] = p;
	this->echiquier[xa][ya][za]->bouge_piece(xa, ya, za);
}

void Echiquier::bouge_piece(int xd, int yd, int zd, int xa,int ya,int za)
{
	delete this->echiquier[xa][ya][za];
	this->echiquier[xa][ya][za] = this->echiquier[xd][yd][zd];
	this->echiquier[xa][ya][za]->bouge_piece(xa, ya, za);
	this->echiquier[xd][yd][zd] = new Piece();
}

Piece* Echiquier::get_piece(int x, int y, int z)
{
	return this->echiquier[x][y][z];
}

int Echiquier::est_vide(int x, int y, int z)
{
	if (x>=0 and x<6 and y>=0 and y<8 and z>=0 and z<6)
	{
		return (-1 == this->get_couleur(x,y,z)) ;
	}
	
	return false;
}

int Echiquier::get_couleur(int x, int y, int z)
{
	if (x>=0 and x<6 and y>=0 and y<8 and z>=0 and z<6)
	{
		return this->echiquier[x][y][z]->get_couleur() ;
	}
	return -1;
}
